﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Specialized;
using Ylh.DAL;

namespace Ylh.Data
{
  public  class RefSelect
    {
        public static DataTable Select(Type T, SqlWhere where, String ConStr)
        {
            TableAttribute TableName;
            ColumnAttribute[] Columns;
            EntityBasePub.GetEntityProperty(T, out TableName, out  Columns);

            NameValueCollection iCmdParam = new NameValueCollection();
            String iSqlWhere = String.Empty;

            EntityBasePub.getParmValue(where, where.Info, Columns, ref iCmdParam, ref  iSqlWhere);
            String StrCmd = @"SELECT {0} FROM {1} WHERE 1=1 {2}";
            String SelectColumn = String.IsNullOrEmpty(where.SelectColumn) ? "*" : where.SelectColumn;
            String StrTableName = TableName.TableName;
            StrCmd = String.Format(StrCmd, SelectColumn, StrTableName, iSqlWhere);
            return DbFactory.CreateInstance().CreateDb().ExecuteQuery(StrCmd, CommandType.Text, iCmdParam, ConStr);
        }

        public static DataTable Select(Type T, QueryPage page, string ConStr)
        {
            //只能支持单表分页
            TableAttribute TableName;
            ColumnAttribute[] Columns;
            EntityBasePub.GetEntityProperty(T, out TableName, out  Columns);
            String StrCmd = @"SELECT {0} FROM {1} WHERE 1=1 AND {2} Order By {3}";
            String SelectColumn = String.IsNullOrEmpty(page.PageSelctColumn) ? "*" : page.PageSelctColumn;
            StrCmd = String.Format(StrCmd, SelectColumn, TableName.TableName, page.Where.StrWhere, page.OrderByColumn);
            DataTable dt = Select(T, page.Where, String.Empty, ConStr);
            page.AllCount = Convert.ToInt32(dt.Rows[0][0]);
            //拼接分页Sql
            StrCmd = SQLPage.MakeSQLStringByPage(StrCmd, page.Where.StrWhere, page.PageSize, page.CurrentPage, page.AllCount);
            return DbFactory.CreateInstance().CreateDb().ExecuteQuery(StrCmd, CommandType.Text, page.Where.PreWhere, ConStr);
        }

        public static DataTable Select(Type T, OQLWhere where, String SelectColumn, String ConStr)
        {
            TableAttribute TableName;
            ColumnAttribute[] Columns;
            EntityBasePub.GetEntityProperty(T, out TableName, out  Columns);
            String StrCmd = @"SELECT {0} FROM {1} WHERE 1=1 AND  {2}";
            SelectColumn = String.IsNullOrEmpty(SelectColumn) ? " Count(*) " : SelectColumn;
            StrCmd = String.Format(StrCmd, SelectColumn, TableName.TableName, where.StrWhere);
            return DbFactory.CreateInstance().CreateDb().ExecuteQuery(StrCmd, CommandType.Text, where.PreWhere, ConStr);
        }


        public static DataTable Select(Type T, OQLWhere where, string ConStr)
        {

            TableAttribute TableName;
            ColumnAttribute[] Columns;
            EntityBasePub.GetEntityProperty(T, out TableName, out  Columns);
            String StrCmd = @"SELECT {0} FROM {1} WHERE 1=1 AND  {2}";
            String SelectColumn = String.Empty;

            foreach (String  Str in where.SelectColumn)
            {
                SelectColumn += Str + ",";
            }

            SelectColumn = String.IsNullOrEmpty(SelectColumn) ? " * " : SelectColumn.TrimEnd(',');

            StrCmd = String.Format(StrCmd, SelectColumn, TableName.TableName, where.StrWhere);
            return DbFactory.CreateInstance().CreateDb().ExecuteQuery(StrCmd, CommandType.Text, where.PreWhere, ConStr);

        }
    }
}
